<?php

use Carbon\Carbon as Carbon;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class RedditController extends BaseController {

  public function startTIR() {
    $userName = trim(Input::get('username'));
    $redditor = Redditor::where('username', '=', $userName)->first();
    if (is_null($redditor)) {
      // save username in magic database
      $redditor           = new Redditor();
      $redditor->username = $userName;

      $validator = Validator::make($redditor->toArray(), Redditor::$rules);
      if ($validator->fails()) {
        Session::flash('error', 'This username "' . $userName . '" seems to be invalid.');
        return View::make('pages.home');
      } else {
        $redditor->save();

        // check if the user exists.
        $reddit = App::make('reddit');
        $result = $reddit->login(Config::get('reddit.username'), Config::get('reddit.password'));
        if ($result === false) {
          Log::debug('Some JSON error I guess. ' . print_r($result, true));
          return App::abort(500);
        }
        $exists               = $reddit->userExists($userName);

        $redditor->userexists = $exists ? 1 : 0;
        $redditor->save();

        if (!$exists) {
          Session::flash('error', 'Reddit has no user called "' . $userName . '".');
          return View::make('pages.home');
        }


        return Redirect::to('/stat/' . $userName);
      }
    } else {
      return Redirect::to('/stat/' . $redditor->username);
    }
  }

  public function stat($userName) {
    // something something overview of user.
    $redditor = Redditor::where('username', '=', $userName)->first();
    if (!$redditor) {
      // create user, then return view
      $redditor           = new Redditor();
      $redditor->username = $userName;
      $validator          = Validator::make($redditor->toArray(), Redditor::$rules);
      if ($validator->fails()) {
        Session::flash('error', 'This username "' . $userName . '" seems to be invalid.');
        return View::make('pages.home');
      } else {
        $redditor->save();
      }
    }
    if (is_null($redditor->userexists)) {
      return View::make('stat.unknown')->with('username', $userName);
    }
    if (intval($redditor->userexists) == 0) {
      Session::flash('error', 'Reddit has no user called "' . $userName . '".');
      return View::make('pages.home');
    }

    // get comments count for some nice stats
    $timeLimit       = new Carbon();
    $timeLimit->subDays(CronController::TIMELIMITDAYS);
    $timeLimitString = $timeLimit->format('Y-m-d');
    $count           = $redditor->comments()->where('startdate', '>=', $timeLimitString)->where('startdate', '!=', '0000-00-00')->count();
    if ($count == 0) {
      return View::make('stat.nocomments')->with('username', $userName);
    } else if (is_null($redditor->timeline)) {
      return View::make('stat.nocomments')->with('username', $userName);
    }
    $data = (string) $redditor->timeline;
    return View::make('stat.stats')->with('username', $userName)->with('count', $count)->with('jsoncode', $data);
  }

}